package TAIC.flicker;

import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

import TAIC.flicker.LM.Stemmer;

public class WordImageDict {
	Stemmer stemmer = new Stemmer ( ); 
	public static int DIM = 2000 ;
	public static int WordDim = 2600 ; 
	public HashMap < String, int[] > featureMap = new HashMap < String, int[]> ();
	public HashMap < String, int []> dict = new HashMap < String, int[]>() ;
	Vector < String > imageNames = new Vector < String > ( 20000 ) ; 
	
	/**
	 * @param args
	 * This program generate the word to image dictionary
	 * 
	 * sample usage: ~ wordMapping.txt IP\svmCurpus SiftFlickerImages.txt StdWord.txt WordImageDict.txt
	 */
	public static void main(String[] args) throws Exception {
		if ( args.length != 5 ) {
			System.out.println ( "please input the wordMapping, the SIFT for all images, image names, wordistFile(output), and the outputDictFile");
			return ;
		}
		
		( new WordImageDict()).work( args ) ; 
	}
	
	public void work ( String [] args) throws Exception {
		Scanner scanner = new Scanner ( new File ( args[ 2 ] )) ;
		while ( scanner.hasNext() ) {
			scanner.next() ;
			scanner.next() ;
			String tmp = scanner.next() ; 
			//System.out.println ( tmp ) ; 
			imageNames.add( tmp.substring(tmp.lastIndexOf('\\') + 1, tmp.lastIndexOf('.'))) ;
		}
		scanner.close() ;
		
		scanner = new Scanner ( new File ( args[ 1 ] )) ;
		int lines = scanner.nextInt () ;
		if ( lines != imageNames.size() ) throw new RuntimeException ("The sift feature file is not consistance with the fileNames") ;
		scanner.nextInt () ;
		for ( int i = 0 ; i < lines; i ++ ) {
			int [] fea = new int [ DIM ] ;
			scanner.next(); scanner.next() ;
			Scanner lineScanner = new Scanner ( scanner.nextLine () ) ; 
			while ( lineScanner.hasNext() ){
				String tmp = lineScanner.next();
				int pos = Integer.valueOf( tmp.substring( 0, tmp.indexOf( ':' ))) ;
				int value = Integer.valueOf( tmp.substring( tmp.indexOf( ':' )+ 1 )) ;
				fea [ pos -1 ] = value ; 
			}
			lineScanner.close();
			featureMap.put( imageNames.get(i), fea ) ;
			
//			for ( int j=  0; j < DIM ; j ++ ) System.out.println ( fea[ j ]) ; 
//			System.exit(0); 
		}
		scanner.close() ;
		
		scanner = new Scanner ( new File ( args [ 0 ] )) ;
		while ( scanner.hasNext() ) {
			String word = stemmer.stem( scanner.next () );
			int count = scanner.nextInt();
			int result [] = dict.get( word ) ; 
			if ( result == null ) result = new int [ DIM ] ;
			for ( int i =0 ; i < count ; i ++ ) { 
				String tImage = scanner.next() ;
				int [] fea;
				if ( (fea = featureMap.get(tImage)) == null ) {continue ;}
				for ( int j = 0 ; j < DIM ; j ++ ) result [ j ] += fea [ j ]; 
			}
			dict.put ( word , result ) ; 
		}
		scanner.close() ;
		
		PrintStream wordOut = new PrintStream ( new File ( args [ 3 ])) ;
		PrintStream fout = new PrintStream ( new File ( args [4 ] )) ;
		fout.println ( dict.size() + " " + DIM ) ; 
		for ( Map.Entry< String,  int[]> entry : dict.entrySet() ) {
			String word = entry.getKey();
			int [] p = entry.getValue() 	;
			wordOut.println( word ) ;
			for ( int i = 0 ; i < DIM ; i ++ ) if ( p[ i ] != 0 ) fout.printf ( "%d:%d ", i, p [ i ]) ;			
			fout.println(); 
		}
		wordOut.close() ;
		fout.close() ; 
	}
	
}
